package com.ZengQi.event.controller;

import com.ZengQi.event.entity.Admin;
import com.ZengQi.event.entity.Publisher;
import com.ZengQi.event.entity.User;
import com.ZengQi.event.service.AdminService;
import com.ZengQi.event.service.PublisherService;
import com.ZengQi.event.service.UserService;
import com.ZengQi.event.service.impl.AdminServiceImpl;
import com.ZengQi.event.service.impl.PublisherServiceImpl;
import com.ZengQi.event.service.impl.UserServiceImpl;
import com.ZengQi.event.view.AdminMainView;
import com.ZengQi.event.view.LoginView;
import com.ZengQi.event.view.PublisherMainView;
import com.ZengQi.event.view.UserMainView;

import javax.swing.*;

public class LoginController {
    private LoginView loginView;
    public LoginController(LoginView loginView){
        this.loginView=loginView;
    }

    //用户登录
    public void login(User conditionUser){
        if (conditionUser.getLoginPnum() == null || "".equals(conditionUser.getLoginPnum().trim()) || conditionUser.getLoginPwd() == null || "".equals(conditionUser.getLoginPwd().trim())) {
            JOptionPane.showMessageDialog(loginView,"手机号或密码不能为空");
            return;

        }
        UserService userService=new UserServiceImpl();
        User user = userService.findUser(conditionUser);
        if(user==null){
            JOptionPane.showMessageDialog(loginView,"手机号或密码错误");
            loginView.getPwdField().setText("");
            loginView.getUserTxt().setText("");


        }else if(user.getLoginPwd().equals(conditionUser.getLoginPwd())){
            //登录成功跳转到主界面
            new UserMainView(user.getUserName());
            loginView.dispose();
        }

    }
    public void login(Admin conditionAdmin){
        if(conditionAdmin.getLoginPnum()==null||"".equals(conditionAdmin.getLoginPnum().trim())||conditionAdmin.getLoginPwd()==null||"".equals(conditionAdmin.getLoginPwd().trim())){
            JOptionPane.showMessageDialog(loginView,"手机号或密码不能为空");
            return;

        }
        AdminService adminService=new AdminServiceImpl();
        Admin admin=adminService.findAdmin(conditionAdmin);
        if(admin==null){
            //跳转到管理员界面
            JOptionPane.showMessageDialog(loginView,"手机号或密码错误");
            loginView.getPwdField().setText("");
            loginView.getUserTxt().setText("");
        }else if(admin.getLoginPwd().equals(conditionAdmin.getLoginPwd())){
            //跳转到管理员界面
            new AdminMainView(admin.getAdminName());
            loginView.dispose();
        }


    }
    public void login(Publisher conditionPublisher){
        if(conditionPublisher.getLoginPnum()==null||"".equals(conditionPublisher.getLoginPnum().trim())||conditionPublisher.getLoginPwd()==null||"".equals(conditionPublisher.getLoginPwd().trim())){
            JOptionPane.showMessageDialog(loginView,"手机号或密码不能为空");
                    return;
        }
        PublisherService publisherService=new PublisherServiceImpl();
        Publisher publisher=publisherService.findPublisher(conditionPublisher);
        if(publisher==null){
            //跳转到出题员界面
            JOptionPane.showMessageDialog(loginView,"手机号或密码错误");
            loginView.getPwdField().setText("");
            loginView.getUserTxt().setText("");
        }else  if(publisher.getLoginPwd().equals(conditionPublisher.getLoginPwd())){
            //跳转到出题员界面
            new PublisherMainView(publisher);
            loginView.dispose();
        }
    }
}
